<template>
  <el-dialog v-model="show" title="平台" class="rounded-[15px]" width="600">
    <el-scrollbar height="500px">
      <el-form :model="drawerProps.row" label-width="120" ref="formRef" :rules="rules">
        <el-form-item label="平台名称" prop="name">
          <el-input v-model="drawerProps.row.name" :input-style="{ width: '250px' }"></el-input>
        </el-form-item>
        <el-form-item label="排序值">
          <el-input-number v-model="drawerProps.row.sort" :input-style="{ width: '250px' }" :min="1" :max="100"></el-input-number>
        </el-form-item>
        <el-form-item label="佣金" prop="settlement">
          <el-input
            v-model="drawerProps.row.settlement"
            type="textarea"
            :input-style="{ width: '250px', height: '150px' }"
            :maxlength="1000"
            :show-word-limit="true"
          ></el-input>
        </el-form-item>
        <el-form-item label="结算条件" prop="commission">
          <el-input
            v-model="drawerProps.row.commission"
            type="textarea"
            :input-style="{ width: '250px', height: '150px' }"
            :maxlength="1000"
            :show-word-limit="true"
          ></el-input>
        </el-form-item>
        <el-form-item label="判客规则" prop="judge_rules">
          <el-input
            v-model="drawerProps.row.judge_rules"
            type="textarea"
            :input-style="{ width: '250px', height: '200px' }"
            :maxlength="1000"
            :show-word-limit="true"
          ></el-input>
        </el-form-item>
      </el-form>
    </el-scrollbar>

    <template #footer>
      <el-button type="primary" @click="submit">确认</el-button>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import { ElMessage, FormInstance, FormRules } from "element-plus";
import { ref } from "vue";
interface Row {
  name: string;
  sort: number;
  settlement: string;
  commission: string;
  lou_pan_id: number;
  judge_rules: string;
}
interface Props {
  row: Row;
  api?: (params: any) => Promise<any>;
  getTable?: () => Promise<any>;
}

const rules: FormRules = {
  name: [{ required: true, max: 10, min: 1, message: "字数在1-10之间" }],
  settlement: [{ required: true, max: 1000, min: 1, message: "字数在1-1000之间" }],
  commission: [{ required: true, max: 1000, min: 1, message: "字数在1-1000之间" }],
  judge_rules: [{ required: true, max: 1000, min: 1, message: "字数在1-1000之间" }]
};

const drawerProps = ref<Props>({ row: { name: "", sort: 100, settlement: "", commission: "", lou_pan_id: 0, judge_rules: "" } });

const show = ref(false);

const open = (params: Props) => {
  drawerProps.value = params;
  show.value = true;
};

const formRef = ref<FormInstance>();
const submit = () => {
  if (!formRef.value) return;
  formRef.value.validate(async valid => {
    if (!valid) return;
    try {
      await drawerProps.value.api!(drawerProps.value.row);
      ElMessage.success("成功");
      await drawerProps.value.getTable!();
      show.value = false;
    } catch (error) {}
  });
};

defineExpose({ open });
</script>

<style></style>
